package com.henu.yizhiya.util;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.henu.R;
import com.henu.yizhiya.db.RoomDB;
import java.util.ArrayList;

/**
 * Created by y on 2017/5/18.
 */

public class RoomListAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<RoomDB> list = new ArrayList<RoomDB>();
    private LayoutInflater inflater;

    //构造方法
    public RoomListAdapter(Context context) {
        this.context= context;
        inflater = LayoutInflater.from(context);
    }

    public ArrayList<RoomDB> getListAll(){
        return list;
    }
    //为适配器提供数据的方法
    public void addAllData(ArrayList<RoomDB> tempList){
        list.addAll(tempList);
    }
    public void addData(RoomDB room){
        list.add(room);
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public RoomDB getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView = inflater.inflate(R.layout.room_item,null);
            holder = new ViewHolder();
            holder.r_num = (TextView) convertView.findViewById(R.id.tv_r_num);
            holder.r_p1 = (TextView) convertView.findViewById(R.id.tv_p1);
            holder.r_p2 = (TextView) convertView.findViewById(R.id.tv_p2);
            holder.r_p3 = (TextView) convertView.findViewById(R.id.tv_p3);
            holder.r_p4 = (TextView) convertView.findViewById(R.id.tv_p4);
            holder.r_p5 = (TextView) convertView.findViewById(R.id.tv_p5);
            holder.r_p6 = (TextView) convertView.findViewById(R.id.tv_p6);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        final RoomDB room = list.get(position);
        holder.r_num.setText(room.getR_num());

        System.out.println("--"+room.getP1()+"--"+room.getP2()+"--"
                +room.getP3()+"--"+room.getP4()+
                "--"+room.getP5()+"--"+room.getP6());

        if (room.getP1().equals("1")){
            holder.r_p1.setBackgroundResource(R.drawable.exist);
        }
        if (room.getP2().equals("1")){
            holder.r_p1.setBackgroundResource(R.drawable.exist);
        }
        if (room.getP3().equals("1")){
            holder.r_p3.setBackgroundResource(R.drawable.exist);
        }
        if (room.getP4().equals("1")){
            holder.r_p4.setBackgroundResource(R.drawable.exist);
        }
        if (room.getP5().equals("1")){
            holder.r_p5.setBackgroundResource(R.drawable.exist);
        }
        if (room.getP6().equals("1")){
            holder.r_p6.setBackgroundResource(R.drawable.exist);
        }
        return convertView;
    }

    class ViewHolder{
        TextView r_num=null;
        TextView r_p1=null;
        TextView r_p2=null;
        TextView r_p3=null;
        TextView r_p4=null;
        TextView r_p5=null;
        TextView r_p6=null;
    }
}
